//
//  LYStudentDataViewController.m
//  artist
//
//  Created by laotang on 16/4/24.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYStudentDataViewController.h"
#import "LYSelectedStudentView.h"
#import "LYStudentInfoModel.h"
#import "LSYUIActionSheet.h"
#import "NSDate+Utils.h"

#import <QBImagePickerController.h>
#import "LSYPhotoAddView.h"
#import "QNUploadManager.h"
#import "QNUploadOption.h"
#import "LYCityDataModel.h"
#import "LYCityDataDBTool.h"
#import "LYSelectedClassesViewController.h"

#import "LYMineIntrestViewController.h"
typedef enum : NSUInteger {
    LYIdentityTypeStudent,
    LYIdentityTypeTeacher,
    LYIdentityTypeOrganization,
} LYIdentityType;


typedef enum : NSUInteger {
    LYStudentSelectedImageTypeIcon,
    LYStudentSelectedImageTypeCers,
} LYStudentSelectedImageType;

@interface LYStudentDataViewController()<UIPickerViewDelegate, UIPickerViewDataSource,UIActionSheetDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, QBImagePickerControllerDelegate>
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, assign) LYIdentityType type;
@property (nonatomic, assign) NSArray *pickDataSouce;
@property (nonatomic, strong) NSMutableArray<LYSelectedStudentViewSelectModel *> *studentArray;
@property (nonatomic, strong) NSMutableArray<LYSelectedStudentViewSelectModel *> *teacherArray;
@property (nonatomic, strong) NSMutableArray<LYSelectedStudentViewSelectModel *> *origanizationArray;
@property (nonatomic, strong) NSMutableArray<LYSelectedStudentViewSelectModel *> *selectedArray;
@property (nonatomic, strong) LYSelectedStudentView *containVeiw;
@property (nonatomic, strong) NSIndexPath *selectedIndexPath;

@property (nonatomic, strong) LYUpdateInfoModel *updateInfoModel;
@property(nonatomic, strong) LSYPhotoAddView *addPhotoView;
@property (strong, nonatomic) NSMutableArray *selectedImageList;
@property (strong, nonatomic) NSMutableArray  *choseBigImageLists;
@property (assign, nonatomic) NSInteger numberOfMaxCountPhoto;
@property (copy, nonatomic) NSString * actionSheetTitle;
@property (nonatomic, strong) NSString *qiNiuToken;
@property (nonatomic, assign) NSInteger uploadIndex; // 当前上传的图片下标
@property (nonatomic, strong) NSMutableString *imageLink;
@property (nonatomic, assign) BOOL ifSelectedCers;
@property (nonatomic, strong) QNUploadManager *upManager;

@property (nonatomic, copy)  void(^uploadImageCompleteHandle)(NSString *);
@property (nonatomic, strong) NSString *selectedProvince;
@property (nonatomic, strong) NSArray <LYCityDataModel *>*cityDataModel;

@property (nonatomic, strong) NSArray *provices;
@property (nonatomic, strong) LYUserInfoModel *userInfo;
@property (nonatomic, assign) BOOL canEidtIndentity;
@property (nonatomic, strong) NSMutableArray *selectSubject;


@end


@implementation LYStudentDataViewController


- (instancetype)initWithCanEditIdentity:(BOOL)isEdit;
{
    self = [self init];
    if(self){
        self.canEidtIndentity = isEdit;
    }
    return self;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    WS(weakSelf)
    [LYRequestTool acquirQiNiusuccess:^(NSString *token) {
        
        weakSelf.qiNiuToken = token;
    } failure:^(NSString *code, NSString *message) {
        
    }];

    self.view.backgroundColor = [UIColor whiteColor];
    self.numberOfMaxCountPhoto = 3;
    self.containVeiw = [[LYSelectedStudentView alloc] initWithFrame:CGRectMake(0, 0, self.view.My_Width, LYScreenHeight)];
    self.updateInfoModel = [[LYUpdateInfoModel alloc] init];
    [self.containVeiw setCellDidPress:^(LYSelectedStudentViewSelectModel *model, NSIndexPath *indexPath) {
        
        // 不能修改身份
        if (self.canEidtIndentity == NO && indexPath.section == 0){
            [SVProgressHUD showErrorWithStatus:@"不能修改身份"];
            return;
        }
        
        [weakSelf.view endEditing:YES];
        weakSelf.selectedIndexPath = indexPath;
        if (model.selectArray){
            [UIView animateWithDuration:0.5 animations:^{
                weakSelf.pickerView.My_Y = LYScreenHeight - 250;
            }];
            weakSelf.pickDataSouce = model.selectArray;
            [weakSelf.pickerView reloadAllComponents];

        }else{
            [UIView animateWithDuration:0.5 animations:^{
                [weakSelf hiddenPickView];
            }];
            
            if (indexPath.section == 1 && indexPath.row == 0){
                [weakSelf addPhotoButtonPressedWithType:LYStudentSelectedImageTypeIcon];
                weakSelf.numberOfMaxCountPhoto = 1;
            }
            
            
            if ([model.title containsString:@"教育方向"] ||
                [model.title containsString:@"艺考方向"] ||
                [model.title containsString:@"培训方向"]){
                weakSelf.selectedIndexPath = indexPath;
                
                
                LYMineIntrestViewController * selectVC = [[LYMineIntrestViewController alloc] init];
                [weakSelf.navigationController pushViewController:selectVC animated:NO];
                selectVC.title = model.title;
                selectVC.selectedMaxCount = 1;

                [selectVC setConfirmSubjectids:^(NSString *selectedSubjected) {
                    weakSelf.updateInfoModel.subject_id = selectedSubjected;
                }];
                weakSelf.selectSubject = selectVC.interests;

            }

        }
    }];
    self.upManager = [[QNUploadManager alloc] init];

    [self.view addSubview:self.containVeiw];
    [self.view addSubview:self.pickerView];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(hiddenPickView)
                                                 name:UIKeyboardWillShowNotification object:nil];
    

    
    
    
    UIBarButtonItem * rightitem = [[UIBarButtonItem alloc] initWithTitle:@"确定"style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonClick)];
    self.selectedImageList = [NSMutableArray array];
    self.choseBigImageLists = [NSMutableArray array];
    self.navigationItem.rightBarButtonItem = rightitem;
    self.containVeiw.tableVeiw.tableFooterView = self.addPhotoView;
    self.addPhotoView.hidden = YES;
    self.title = @"完善信息";
    [SVProgressHUD showWithStatus:@"正在获取个人信息"];
    [self requestUserInfoData];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    [self getData];
    
    NSArray *cells = [self.containVeiw.tableVeiw visibleCells];
    WS(weakSelf)
    [cells enumerateObjectsUsingBlock:^(LYSelectedClassInfoTableViewCell *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        
        
        if ([obj.selectModel.title containsString:@"教育方向"] ||
            [obj.selectModel.title containsString:@"艺考方向"] ||
            [obj.selectModel.title containsString:@"培训方向"]){
            obj.selectModel.originProperty = weakSelf.userInfo.extra_info.major;
            obj.selectModel = obj.selectModel;
            
        }
    }];
//
//    
//     // 垃圾代码, 只是为了实现功能
//    switch (self.userInfo.role_id ) {
//        case 1: {
//            self.studentArray = nil;
//            self.selectedArray = self.studentArray;
//            self.addPhotoView.hidden = YES;
//            
//        }
//            break;
//        case 2:
//        {
//            self.teacherArray = nil;
//            self.selectedArray = self.teacherArray;
//            self.addPhotoView.hidden = NO;
//            
//        }
//            break;
//        case 3:
//        {
//            self.origanizationArray = nil;
//            self.selectedArray = self.origanizationArray;
//            self.addPhotoView.hidden = NO;
//            
//        }
//            break;
//            
//    }
//    [self.containVeiw.tableVeiw reloadData];



}
- (void)requestUserInfoData{
    
    WS(weakSelf)
    [LYRequestTool acquireUserInfoWithToken:[AppDelegate shareInstance].token  success:^(LYUserInfoModel * userModel) {
        [SVProgressHUD dismiss];
        
        
        weakSelf.userInfo = userModel;

        switch (weakSelf.userInfo.role_id ) {

            case 1: {
                self.selectedArray = self.studentArray;
                self.addPhotoView.hidden = YES;

            }
                break;
            case 2:
            {
                self.selectedArray = self.teacherArray;
                self.addPhotoView.hidden = NO;

            }
                break;
            case 3:
            {
                self.selectedArray = self.origanizationArray;
                self.addPhotoView.hidden = NO;

            }
                break;

        }
        weakSelf.containVeiw.userInfo = userModel;
        weakSelf.containVeiw.selectModel = weakSelf.selectedArray;
        [weakSelf.containVeiw.tableVeiw reloadData];

        NSLog(@"=====================%@",userModel.extra_info.cers);
        
        
        if (userModel.extra_info.cers.length > 0){
            NSArray *urls =  [userModel.extra_info.cers componentsSeparatedByString:@","];
            [weakSelf downloadImageWithurlArray:urls];
        }



    } failure:^(NSString *code, NSString *message) {
        [SVProgressHUD showErrorWithStatus:message];
    }];
    
    
    
    
    
}


- (void)downloadImageWithurlArray:(NSArray *)imageurls{
    
    WS(weakSelf)
    [imageurls enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:obj] options:SDWebImageRetryFailed progress:nil  completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            
            if (!error && image) {
                [weakSelf.addPhotoView.imageUrlArray addObject:image];
                [weakSelf.addPhotoView resetPhotoView];
                [weakSelf.choseBigImageLists addObject:image];

            }
            
        }];
    }];
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}

- (void)hiddenPickView {
    
        self.pickerView.My_Y = LYScreenHeight;
}
- (LSYPhotoAddView *)addPhotoView{
    
    if(!_addPhotoView){
        _addPhotoView = [[LSYPhotoAddView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth, 150)];
        WS(weakSelf)
        [_addPhotoView setAddButtonClickBlock:^{
            [weakSelf addPhotoButtonPressedWithType:LYStudentSelectedImageTypeCers];
        }];
        [_addPhotoView setDelButtonClickBlock:^(NSInteger index) {
            [weakSelf.choseBigImageLists removeObjectAtIndex:index];
            weakSelf.addPhotoView.imageUrlArray = weakSelf.choseBigImageLists;
            [weakSelf.addPhotoView resetPhotoView];
        }];
        
    }
    return _addPhotoView;
}
- (void)addPhotoButtonPressedWithType:(LYStudentSelectedImageType)type
{
    
    if (type == LYStudentSelectedImageTypeCers){
        self.numberOfMaxCountPhoto = 3;
        self.actionSheetTitle = @"请选择证书";
        self.ifSelectedCers = YES;

    }else{
        self.numberOfMaxCountPhoto = 1;
        self.actionSheetTitle = @"请选择头像";
        self.ifSelectedCers = NO;
    }
    
    self.selectedIndexPath = nil;

    NSArray *imagesArray = _selectedImageList;
    if (imagesArray.count >= 9) {
        NSString *tmpStr = [NSString stringWithFormat:@"图片不能超过%zd张",self.numberOfMaxCountPhoto];
        [SVProgressHUD showErrorWithStatus:tmpStr];
        return;
    }
    
    LSYUIActionSheet* addPicActionSheet = [[LSYUIActionSheet alloc]initWithTitle:self.actionSheetTitle delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从手机相册选择", nil];
    addPicActionSheet.tag = 1000;
    [addPicActionSheet showInNoErrorView:self.view];
}


-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag ==1000) {
        
        if (buttonIndex == 0) {
            //拍照
            if (![LYPublicMethod checkCameraAuthorizationStatus]) {
                return;
            }
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
//            picker.allowsEditing = YES;//设置可编辑
            picker.allowsEditing = !self.ifSelectedCers;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:picker animated:YES completion:nil];//进入照相界面
        }
        else if (buttonIndex == 1){
            //相册
            if (![LYPublicMethod checkPhotoLibraryAuthorizationStatus]) {
                return;
            }
            if (self.ifSelectedCers){
                QBImagePickerController *imagePickerController = [[QBImagePickerController alloc] init];
                imagePickerController.delegate = self;
                imagePickerController.allowsMultipleSelection = YES;
                //            imagePickerController.allowsEditing = !self.ifSelectedCers;
                
                imagePickerController.allowsMultipleSelection = YES;
                imagePickerController.maximumNumberOfSelection = self.numberOfMaxCountPhoto - self.choseBigImageLists.count;
                [self presentViewController:imagePickerController animated:YES completion:NULL];
            }else{
                UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                picker.delegate = self;
                //            picker.allowsEditing = YES;//设置可编辑
                picker.allowsEditing = !self.ifSelectedCers;
                picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentViewController:picker animated:YES completion:nil];//进入照相界面
            }
            
        }
    }
}

/**
 *  相册选择回调
 */
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets
{
    [self dealWithSelectAssets:assets];
    
}
- (BOOL)qb_imagePickerController:(QBImagePickerController *)imagePickerController shouldSelectAsset:(ALAsset *)asset
{
    NSMutableOrderedSet *orderSet = [imagePickerController selectedAssetURLs];
    NSInteger currentCount = (orderSet.count + self.choseBigImageLists.count);
    if (currentCount >= self.numberOfMaxCountPhoto) {
        [self showNoCanSelectImageAler];
        return NO;
    }
    return YES;
}

- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
    
    
    if (_ifSelectedCers){
        [self.choseBigImageLists addObject:image];
        self.addPhotoView.imageUrlArray = self.choseBigImageLists;
        [self resetPhotoViewByImageChange];
    }
    
    

    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage] ;
    
    if (!self.ifSelectedCers){
        LYSelectedClassInfoTableViewCell * cell = [self.containVeiw.tableVeiw cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
        cell.placeHoldImageView.hidden = NO;
        cell.placeHoldImageView.image = image;
        cell.textFeild.hidden = YES;
    }else{
        
        [self.choseBigImageLists addObject:image];
        self.addPhotoView.imageUrlArray = self.choseBigImageLists;
        [self resetPhotoViewByImageChange];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (UIImage *)fullResolutionImageFromALAsset:(ALAsset *)asset{
    ALAssetRepresentation *assetRep = [asset defaultRepresentation];
    CGImageRef imgRef = [assetRep fullResolutionImage];
    UIImage *img = [UIImage imageWithCGImage:imgRef scale:assetRep.scale orientation:(UIImageOrientation)assetRep.orientation];
    return img;
}

- (void)resetPhotoViewByImageChange
{
    UIImageView *image = [[UIImageView alloc]init];
    [image sd_setImageWithURL:nil placeholderImage:nil];
    
    [self.addPhotoView resetPhotoView];
}
- (void)dealWithSelectAssets:(NSArray *)assets{
    
    
    if (self.ifSelectedCers){
        
        for (ALAsset *assetItem in assets) {
            UIImage *highQualityImage = [self fullResolutionImageFromALAsset:assetItem];
            [self.choseBigImageLists addObject:highQualityImage];
            
        }
        
        if (self.selectedIndexPath){
            LYSelectedClassInfoTableViewCell * cell = [self.containVeiw.tableVeiw cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
            cell.placeHoldImageView.hidden = NO;
            cell.placeHoldImageView.image = [self.choseBigImageLists firstObject];
            cell.textFeild.hidden = YES;
        }
        
        self.addPhotoView.imageUrlArray = self.choseBigImageLists;
        [self resetPhotoViewByImageChange];
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)showNoCanSelectImageAler
{
    NSString *notice = [NSString stringWithFormat:@"最多可选%@张图片",[@(self.numberOfMaxCountPhoto)stringValue]];
    UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"提示" message:notice delegate:self cancelButtonTitle:@"知道啦!" otherButtonTitles:nil, nil];
    [aler show];
}

- (UIPickerView *)pickerView{
    
    if(!_pickerView){
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, LYScreenHeight, LYScreenWidth, 200)];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
    }
    
    [self.view bringSubviewToFront:_pickerView];
    return _pickerView;
}

- (void)upateDataSourceWithType:(LYIdentityType)type{
    _type  =type;
    switch (type) {
        case LYIdentityTypeStudent:
            self.selectedArray = self.studentArray;
            break;
        case LYIdentityTypeTeacher:
            self.selectedArray = self.teacherArray;
            
            break;
        case LYIdentityTypeOrganization:
            self.selectedArray = self.origanizationArray;
            
        default:  break;
    }
    self.containVeiw.selectModel = self.selectedArray;
    [self.containVeiw.tableVeiw reloadData];
    
}

#pragma mark - delegate

#pragma mark - pickerView

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{


        return 1;
    
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

        return self.pickDataSouce.count;

}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {

    return LYScreenWidth;

}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    
    return 40;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSString *title = @"";

        
    title = self.pickDataSouce[row];
  
    return title;

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    NSString *title = @"";

    if (row < self.pickDataSouce.count){
        title = self.pickDataSouce[row];
    }
    

    
    if (self.selectedIndexPath.section == 0){
        
        switch (row) {
            case 0:{
                self.selectedArray = self.studentArray;
                self.addPhotoView.hidden = YES;
            }
                break;
            case 1:{
                self.selectedArray = self.teacherArray;
                self.addPhotoView.hidden = NO;
            }
                
                break;
            case 2:{
                self.selectedArray = self.origanizationArray;
                self.addPhotoView.hidden = NO;

            }
                break;
        }
        
        self.containVeiw.selectModel = self.selectedArray;
        [self.containVeiw.tableVeiw reloadData];
        
        LYSelectedClassInfoTableViewCell * cell = [self.containVeiw.tableVeiw cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        cell.placeHoldImageView.hidden = YES;
        cell.textFeild.text = title;
        
    }
    
    LYSelectedClassInfoTableViewCell * cell = [self.containVeiw.tableVeiw cellForRowAtIndexPath:self.selectedIndexPath];
    cell.placeHoldImageView.hidden = YES;
    cell.textFeild.text = title;
    cell.textFeild.hidden = NO;
    [self hiddenPickView];

    
}




#pragma dataSource
- (NSMutableArray<LYSelectedStudentViewSelectModel *> *)studentArray{
    if (_studentArray == nil){
        _studentArray = [NSMutableArray array];
        [_studentArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"头像" palceHold:nil selectedArray:nil originProperty: self.userInfo.head_pic]];
       [_studentArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"姓名" palceHold:@"你叫什么名字" selectedArray:nil originProperty:self.userInfo.nick_name]];
        [_studentArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"性别" palceHold:@"男" selectedArray:@[@"男", @"女"] originProperty:self.userInfo.sex]];
       [_studentArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"艺考方向" palceHold:@"学的是什么专业" selectedArray:nil originProperty:self.userInfo.extra_info.major]];
        [_studentArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"年级" palceHold:@"哪一届毕业" selectedArray:nil originProperty:self.userInfo.extra_info.grade]];
        [_studentArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"培训经历" palceHold:@"参加过培训没" selectedArray:nil originProperty:self.userInfo.extra_info.training]];
        [_studentArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"省份" palceHold:@"哪里的呀" selectedArray:[LYCityDataDBTool getProvinces] originProperty:self.userInfo.province]];
        
        [_studentArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"目标院校" palceHold:@"有没有理想的院校" selectedArray:nil originProperty:self.userInfo.extra_info.target_school] ];
        [_studentArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"目标院校分数" palceHold:@"输入文化分" selectedArray:nil originProperty:self.userInfo.extra_info.target_score]];
    }
    return _studentArray;
    
}
- (NSMutableArray<LYSelectedStudentViewSelectModel *> *)teacherArray{
    if (_teacherArray == nil){
        _teacherArray = [NSMutableArray array];

        [_teacherArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"头像" palceHold:nil selectedArray:nil originProperty: self.userInfo.head_pic]];
        [_teacherArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"姓名" palceHold:@"你叫什么名字" selectedArray:nil originProperty:self.userInfo.nick_name]];
        [_teacherArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"性别" palceHold:@"男" selectedArray:@[@"男", @"女"] originProperty:self.userInfo.sex]];
        

        
        LYUserExtraInfoModel * info = self.userInfo.extra_info;
        NSLog(@"%@", self.userInfo.extra_info.major);
        
        [_teacherArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"教育经验" palceHold:@"参加教育多少年" selectedArray:nil originProperty:self.userInfo.extra_info.experience]];
        NSString *major = self.userInfo.extra_info.major;
        [_teacherArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"教育方向" palceHold:@"教的是什么专业" selectedArray:nil originProperty:major]];
        
        [_teacherArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"省份" palceHold:@"哪里的呀" selectedArray:[LYCityDataDBTool getProvinces] originProperty:self.userInfo.province]];
        [_teacherArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"在职院校" palceHold:@"在哪个学校教学" selectedArray:nil originProperty:self.userInfo.extra_info.school]];
    }
    return _teacherArray;
    
}

- (NSMutableArray<LYSelectedStudentViewSelectModel *> *)origanizationArray{
    if (_origanizationArray == nil){
        _origanizationArray = [NSMutableArray array];
        [_origanizationArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"头像" palceHold:nil selectedArray:nil  originProperty:self.userInfo.head_pic]];
        [_origanizationArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"姓名" palceHold:@"你叫什么名字" selectedArray:nil originProperty:self.userInfo.nick_name]];
        [_origanizationArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"培训方向" palceHold:@"培训什么专业" selectedArray:nil originProperty:self.userInfo.extra_info.major]];
        [_origanizationArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"培训经历" palceHold:@"做培训多少年" selectedArray:nil originProperty:self.userInfo.extra_info.experience]];
        [_origanizationArray addObject:[LYSelectedStudentViewSelectModel setTitle:@"省份" palceHold:@"哪里的呀" selectedArray:[LYCityDataDBTool getProvinces] originProperty:self.userInfo.province]];
    }
    return _origanizationArray;
    
}


- (void)getData{
    
    
    NSArray *cellArray =  self.containVeiw.tableVeiw.visibleCells;
    
    [cellArray enumerateObjectsUsingBlock:^(LYSelectedClassInfoTableViewCell * cell, NSUInteger idx, BOOL * _Nonnull stop) {
        
        LYSelectedStudentViewSelectModel *selectModel = cell.selectModel;
        if ([selectModel.title isEqualToString:@"头像"]){
            self.updateInfoModel.head_pic = cell.textFeild.text;
        }else if([selectModel.title isEqualToString:@"姓名"]) {
            self.updateInfoModel.nick_name = cell.textFeild.text;

        }else if([selectModel.title isEqualToString:@"性别"]) {
            
            if ([cell.textFeild.text isEqualToString:@"女"]){
                self.updateInfoModel.sex = @"2";
            }else{
                self.updateInfoModel.sex = @"1";
            }

        }else if([selectModel.title isEqualToString:@"艺考方向"]) {
//            self.updateInfoModel.major = cell.textFeild.text;
            self.userInfo.extra_info.major = [self majorWithSelectedWithSelectedSubjects:self.selectSubject];
            self.updateInfoModel.major = [self majorWithSelectedWithSelectedSubjects:self.selectSubject];
        }else if([selectModel.title isEqualToString:@"年级"]) {
            self.updateInfoModel.grade = cell.textFeild.text;
        }else if([selectModel.title isEqualToString:@"培训经历"]) {
            self.updateInfoModel.training = cell.textFeild.text;
        }else if([selectModel.title isEqualToString:@"省份"]) {
           int ID =  [LYCityDataDBTool getIDWithName:cell.textFeild.text];
            self.updateInfoModel.province_id = [NSString stringWithFormat:@"%zd", ID];
        }else if([selectModel.title isEqualToString:@"目标院校"]) {
            self.updateInfoModel.target_school = cell.textFeild.text;

        }else if([selectModel.title isEqualToString:@"目标院校分数"]) {
            self.updateInfoModel.target_score = cell.textFeild.text;

        }else if([selectModel.title isEqualToString:@"教育方向"]) {
            
            self.userInfo.extra_info.major = [self majorWithSelectedWithSelectedSubjects:self.selectSubject];
            
            NSLog(@"%@",self.userInfo.extra_info.major);
            self.updateInfoModel.major = [self majorWithSelectedWithSelectedSubjects:self.selectSubject];

        }else if([selectModel.title isEqualToString:@"教育经验"]) {
            self.updateInfoModel.experience = cell.textFeild.text;

        }else if([selectModel.title isEqualToString:@"在职院校"]) {
            self.updateInfoModel.school = cell.textFeild.text;

        }else if([selectModel.title isEqualToString:@"培训方向"]) {
            self.userInfo.extra_info.major = [self majorWithSelectedWithSelectedSubjects:self.selectSubject];
            self.updateInfoModel.major = [self majorWithSelectedWithSelectedSubjects:self.selectSubject];

        }else if([selectModel.title isEqualToString:@"选择你的身份"]) {
            self.updateInfoModel.role_id = cell.textFeild.text;
            if([cell.textFeild.text isEqualToString:@"老师"]){
                self.updateInfoModel.role_id = @"2";
            }else if ([cell.textFeild.text isEqualToString:@"机构"]){
                self.updateInfoModel.role_id = @"3";
            }else{
                self.updateInfoModel.role_id = @"1";
            }
        }

        
    }];
}

- (NSString *)majorWithSelectedWithSelectedSubjects:(NSArray *)selectedSubjects{
    
   return  [self.selectSubject componentsJoinedByString:@","];

}

- (void)rightButtonClick{
    
    [self getData];




    if(self.choseBigImageLists.count){
        WS(weakSelf)
        self.imageLink = [NSMutableString string];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showWithStatus:@"正在更新资料"];

        [self setUploadImageCompleteHandle:^(NSString *imageLink) {
            if (imageLink.length > 1){
                NSString *imageLink =  [weakSelf.imageLink substringToIndex:(self.imageLink.length - 1)];
                weakSelf.updateInfoModel.cers = imageLink;
                [weakSelf uploadUserIconWithSuccess:^{
                    [weakSelf uploadPersonInfo];
                }];
            }

        }];
        [self uploadImagesuccess:nil];
    }else{ // 没有证书
        
        WS(weakSelf)
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showWithStatus:@"正在更新资料"];
        [weakSelf uploadUserIconWithSuccess:^{
            [weakSelf uploadPersonInfo];
        }];
    }
}


- (void)uploadPersonInfo{
    
    WS(weakSelf)
    weakSelf.updateInfoModel.token = [AppDelegate shareInstance].token;
//    weakSelf.userInfo.extra_info.subject_id = weakSelf.upManager
    [LYRequestTool setPersonalInfo:weakSelf.updateInfoModel success:^{

        [SVProgressHUD showSuccessWithStatus:@"更新资料成功"];
        
        [LYRequestTool acquireUserInfoWithToken:[AppDelegate shareInstance].token  success:^(LYUserInfoModel * userModel) {
            [VVYSaveDataManager saveDataWithData:userModel key:KEY_USERMESSAGE];
            [[LYDataSourceManager sharedManager] resetUserInfo];
            [[VVYCheckStatus sharedInstance] checkIsLogin];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } failure:^(NSString *code, NSString *message) {
            [SVProgressHUD showErrorWithStatus:message];
        }];
        
        
    } failure:^(NSString *code, NSString *message) {
    
        [SVProgressHUD showErrorWithStatus:message];

    }];
    
    

    
}
- (void)uploadUserIconWithSuccess:(void(^)())success{
    
//    [SVProgressHUD showSuccessWithStatus:@"正在上传头像"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [SVProgressHUD showWithStatus:@"正在上传信息"];
    
    LYSelectedClassInfoTableViewCell * cell = [self.containVeiw.tableVeiw cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];

    if(cell.placeHoldImageView.image){
        WS(weakSelf)
        NSData *data = UIImageJPEGRepresentation(cell.placeHoldImageView.image, 0.3);
        [self.upManager putData:data key:nil token:self.qiNiuToken complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
            NSString *urlString= [NSString stringWithFormat:@"http://7xrlrd.com1.z0.glb.clouddn.com/%@", resp[@"key"]];
            weakSelf.updateInfoModel.head_pic = urlString;
            if (success){
                success();
            }
            
        } option:nil];
    }else{
        if (success){
            success();
        }
    }
    

}


- (void)uploadImagesuccess:(void (^)(NSString * imageLink))success
{
    
    if (self.uploadIndex == self.choseBigImageLists.count  ){
        return;
    }

    UIImage *obj = self.choseBigImageLists[self.uploadIndex];
    NSData *data = UIImageJPEGRepresentation(obj, 0.3);
//    NSString *name = [[NSDate date] string];
//    NSString *fileName = [NSString stringWithFormat:@"%@%zd.png",name,self.uploadIndex];
//    NSString *urlString= [NSString stringWithFormat:@"http://7xrlrd.com1.z0.glb.clouddn.com/%@",fileName];
    WS(weakSelf)
    QNUploadOption *option = [[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
        
        NSString *message = [NSString stringWithFormat:@"正在上传%zd/%zd张", weakSelf.uploadIndex + 1, weakSelf.choseBigImageLists.count];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showProgress:percent status:message];


    }];
    
    [self.upManager putData:data key:nil token:self.qiNiuToken complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        NSLog(@"上传成功");
        NSString *url = [NSString stringWithFormat:@"http://7xrlrd.com1.z0.glb.clouddn.com/%@,",resp[@"key"]];
        [weakSelf.imageLink appendString:url];
        if (weakSelf.uploadIndex + 1 >= weakSelf.choseBigImageLists.count){ // 最后一样
            if (weakSelf.uploadImageCompleteHandle){
                [SVProgressHUD dismiss];

                weakSelf.uploadImageCompleteHandle(weakSelf.imageLink);
            }
        }else{
            weakSelf.uploadIndex ++;
            [weakSelf uploadImagesuccess: nil];
        }
    } option:option];
    
    
}

@end
